package simple.framework.util;

import java.io.*;
import java.security.*;
import java.util.regex.*;
import java.net.URLEncoder;

public class encode
{
  public static String addslashes(String argString)
  {
    String tString = argString;
    tString = tString.replace("'", "''");
    tString = tString.replace("\\", "\\\\");
    return tString;
  }

  public static String base64encode(byte[] argData)
  {
    String tmpstr = "";
    byte[] tData = argData;
    char[] base64EncodeChars = new char[] {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
    StringBuffer tStringBuffer = new StringBuffer();
    int tLength = tData.length;
    int ti = 0;
    int tb1, tb2, tb3;
    while (ti < tLength)
    {
      tb1 = tData[ti++] & 0xff;
      if (ti == tLength)
      {
        tStringBuffer.append(base64EncodeChars[tb1 >>> 2]);
        tStringBuffer.append(base64EncodeChars[(tb1 & 0x3) << 4]);
        tStringBuffer.append("=="); 
        break; 
      } 
      tb2 = tData[ti++] & 0xff; 
      if (ti == tLength) 
      {
        tStringBuffer.append(base64EncodeChars[tb1 >>> 2]);
        tStringBuffer.append(base64EncodeChars[((tb1 & 0x03) << 4) | ((tb2 & 0xf0) >>> 4)]);
        tStringBuffer.append(base64EncodeChars[(tb2 & 0x0f) << 2]);
        tStringBuffer.append("=");
        break;
      }
      tb3 = tData[ti++] & 0xff;
      tStringBuffer.append(base64EncodeChars[tb1 >>> 2]);
      tStringBuffer.append(base64EncodeChars[((tb1 & 0x03) << 4) | ((tb2 & 0xf0) >>> 4)]);
      tStringBuffer.append(base64EncodeChars[((tb2 & 0x0f) << 2) | ((tb3 & 0xc0) >>> 6)]);
      tStringBuffer.append(base64EncodeChars[tb3 & 0x3f]);
    }
    tmpstr = tStringBuffer.toString();
    return tmpstr;
  }

  public static byte[] base64decode(String argString) throws UnsupportedEncodingException
  {
    byte[] tByte = null;
    String tString = argString;
    byte[] base64DecodeChars = new byte[] {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1};
    StringBuffer tStringBuffer = new StringBuffer();
    byte[] tData = tString.getBytes("US-ASCII");
    int tLength = tData.length;
    int ti = 0;
    int tb1, tb2, tb3, tb4;
    while (ti < tLength)
    {
      do
      {
        tb1 = base64DecodeChars[tData[ti++]];
      } while (ti < tLength && tb1 == -1);
      if (tb1 == -1) break;
      do
      {
        tb2 = base64DecodeChars[tData[ti++]];
      } while (ti < tLength && tb2 == -1);
      if (tb2 == -1) break;
      tStringBuffer.append((char)((tb1 << 2) | ((tb2 & 0x30) >>> 4)));
      do
      {
        tb3 = tData[ti++];
        if (tb3 == 61)
        {
          tByte = tStringBuffer.toString().getBytes("ISO-8859-1");
          return tByte;
        }
        tb3 = base64DecodeChars[tb3];
      } while (ti < tLength && tb3 == -1);
      if (tb3 == -1) break;
      tStringBuffer.append((char)(((tb2 & 0x0f) << 4) | ((tb3 & 0x3c) >>> 2)));
      do
      {
        tb4 = tData[ti++];
        if (tb4 == 61)
        {
          tByte = tStringBuffer.toString().getBytes("ISO-8859-1");
          return tByte;
        }
        tb4 = base64DecodeChars[tb4];
      } while (ti < tLength && tb4 == -1);
      if (tb4 == -1) break;
      tStringBuffer.append((char)(((tb3 & 0x03) << 6) | tb4));
    }
    tByte = tStringBuffer.toString().getBytes("ISO-8859-1");
    return tByte;
  }

  public static String cachenameencode(String argString)
  {
    String tString = argString;
    tString = tString.replace("/", "_");
    return tString;
  }

  public static String cdatadecode(String argString)
  {
    String tString = argString;
    if (!cls.isEmpty(tString))
    {
      tString = tString.replace("$CDATA#", "<![CDATA[");
      tString = tString.replace("#CDATA$", "]]>");
    }
    return tString;
  }

  public static String encodeNewline(String argString)
  {
    String tString = argString;
    tString = tString.replace(String.valueOf((char)13) + String.valueOf((char)10), String.valueOf((char)10));
    tString = tString.replace(String.valueOf((char)10), String.valueOf((char)13) + String.valueOf((char)10));
    return tString;
  }

  public static String encodeArticle(String argString)
  {
    String tString = argString;
    tString = encodeNewline(tString);
    tString = tString.replace(String.valueOf((char)39), "&#39;");
    tString = tString.replace(String.valueOf((char)32) + String.valueOf((char)32), "&nbsp;&nbsp;");
    tString = tString.replace(String.valueOf((char)13) + String.valueOf((char)10), "<br />");
    return tString;
  }

  public static String encodeChar2String(String argString)
  {
    String tString = argString;
    String tmpString = "";
    if (!cls.isEmpty(tString))
    {
      String[] tAry = tString.split(Pattern.quote(","));
      for (int ti = 0; ti < tAry.length; ti ++)
      {
        int tNumber = cls.getNum(tAry[ti], 0);
        tmpString += String.valueOf((char)tNumber);
      }
    }
    return tmpString;
  }

  public static String encodeHtml(String argString)
  {
    String tString = argString;
    tString = tString.replace("&", "&amp;");
    tString = tString.replace(">", "&gt;");
    tString = tString.replace("<", "&lt;");
    tString = tString.replace("\"", "&quot;");
    return tString;
  }

  public static String encodeText(String argString)
  {
    String tString = argString;
    tString = tString.replace("$", "&#36;");
    tString = tString.replace("@", "&#64;");
    return tString;
  }

  public static String htmlencode(String argString)
  {
    String tmpstr = "";
    String tString = argString;
    tmpstr = htmlencode(tString, "0");
    return tmpstr;
  }

  public static String htmlencode(String argString, String argType)
  {
    String tType = argType;
    String tString = argString;
    tString = cls.getString(tString);
    tString = encodeHtml(tString);
    tString = encodeText(tString);
    if (tType.equals("1"))
    {
      tString = tString.replace("|", "&#5;");
      tString = tString.replace("=", "&#61;");
    }
    return tString;
  }

  public static String keywordencode(String argString)
  {
    String tmpstr = "";
    String tString = argString;
    if (!cls.isEmpty(tString))
    {
      String[] tStringAry = tString.split(Pattern.quote(","));
      for (int ti = 0; ti < tStringAry.length; ti ++) tmpstr += "|:|" + tStringAry[ti] + "|:|,";
      if (!cls.isEmpty(tmpstr)) tmpstr = cls.getLRStr(tmpstr, ",", "leftr");
    }
    return tmpstr;
  }

  public static String keyworddecode(String argString)
  {
    String tmpstr = "";
    String tString = argString;
    if (!cls.isEmpty(tString))
    {
      String[] tStringAry = tString.split(Pattern.quote(","));
      for (int ti = 0; ti < tStringAry.length; ti ++) tmpstr += cls.getLRStr(cls.getLRStr(tStringAry[ti], "|:|", "leftr"), "|:|", "rightr") + ",";
      if (!cls.isEmpty(tmpstr)) tmpstr = cls.getLRStr(tmpstr, ",", "leftr");
    }
    return tmpstr;
  }

  public static String md5(byte[] argSource)
  {
    String tmpstr = "";
    byte[] tSource = argSource;
    char thexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    try
    {
      MessageDigest tMessageDigest = MessageDigest.getInstance("MD5");
      tMessageDigest.update(tSource);
      byte tByte[] = tMessageDigest.digest();
      char tChar[] = new char[16 * 2];
      int tk = 0;
      for (int ti = 0; ti < 16; ti ++)
      {
        byte tBytes = tByte[ti];
        tChar[tk ++] = thexDigits[tBytes >>> 4 & 0xf];
        tChar[tk ++] = thexDigits[tBytes & 0xf];
      }
      tmpstr = new String(tChar);
    }
    catch(Exception e) {}
    return tmpstr;
  }

  public static String scriptencode(String argString)
  {
    String tString = argString;
    tString = tString.replace("\\", "\\\\");
    tString = tString.replace("'", "\\'");
    tString = tString.replace("\"", "\\\"");
    tString = encodeText(tString);
    return tString;
  }

  public static String unescape(String argString)
  { 
    String tmpstr = "";
    String tString = argString;
    if (!cls.isEmpty(tString))
    {
      byte[] tVal = {0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F};
      StringBuffer tStringBuffer = new StringBuffer();
      int ti = 0;
      int tLength = tString.length();
      while (ti < tLength)
      {
        int tCh = tString.charAt(ti);
        if ('A' <= tCh && tCh <= 'Z')
        {
          tStringBuffer.append((char)tCh);
        }
        else if ('a' <= tCh && tCh <= 'z')
        {
          tStringBuffer.append((char)tCh);
        }
        else if ('0' <= tCh && tCh <= '9')
        {
          tStringBuffer.append((char)tCh);
        }
        else if (tCh == '-' || tCh == '_'|| tCh == '.' || tCh == '!' || tCh == '~' || tCh == '*'|| tCh == '\'' || tCh == '(' || tCh == ')')
        {
          tStringBuffer.append((char)tCh);
        }
        else if (tCh == '%')
        {
          int tCint = 0;
          if ('u' != tString.charAt(ti + 1))
          {
            tCint = (tCint << 4) | tVal[tString.charAt(ti + 1)];
            tCint = (tCint << 4) | tVal[tString.charAt(ti + 2)];
            ti += 2;
          }
          else
          {
            tCint = (tCint << 4) | tVal[tString.charAt(ti + 2)];
            tCint = (tCint << 4) | tVal[tString.charAt(ti + 3)];
            tCint = (tCint << 4) | tVal[tString.charAt(ti + 4)];
            tCint = (tCint << 4) | tVal[tString.charAt(ti + 5)];
            ti += 5;
          }
          tStringBuffer.append((char)tCint);
        }
        else
        {
          tStringBuffer.append((char)tCh);
        }
        ti ++;
      }
      tmpstr = tStringBuffer.toString();
    }
    return tmpstr;
  }

  public static String ubb2html(String argString)
  {
    String tString = argString;
    if (!cls.isEmpty(tString))
    {
      Boolean tState = true;
      tString = htmlencode(tString);
      tString = tString.replace(" ", "&nbsp;");
      tString = tString.replace("&nbsp;&nbsp;", "[&nbsp-nbsp;]");
      tString = tString.replace("&nbsp;", " ");
      tString = tString.replace("[&nbsp-nbsp;]", "&nbsp; ");
      tString = tString.replaceAll("\\[br\\]", "<br />");
      String[][] tRegexAry = {
        {"\\[p\\](.*?)\\[\\/p\\]", "$1<br />"},
        {"\\[b\\](.*?)\\[\\/b\\]", "<b>$1</b>"},
        {"\\[i\\](.*?)\\[\\/i\\]", "<i>$1</i>"},
        {"\\[u\\](.*?)\\[\\/u\\]", "<u>$1</u>"},
        {"\\[ol\\]([\\s\\S]*)\\[\\/ol\\]", "<ol>$1</ol>"},
        {"\\[ul\\]([\\s\\S]*)\\[\\/ul\\]", "<ul>$1</ul>"},
        {"\\[li\\](.*?)\\[\\/li\\]", "<li>$1</li>"},
        {"\\[code\\]([\\s\\S]+?)\\[\\/code\\]", "<div class=\"ubb_code\">$1</div>"},
        {"\\[quote\\]([\\s\\S]+?)\\[\\/quote\\]", "<div class=\"ubb_quote\">$1</div>"},
        {"\\[color=([^\\]]*)\\](.*?)\\[\\/color\\]", "<font style=\"color: $1\">$2</font>"},
        {"\\[hilitecolor=([^\\]]*)\\](.*?)\\[\\/hilitecolor\\]", "<font style=\"background-color: $1\">$2</font>"},
        {"\\[align=([^\\]]*)\\](.*?)\\[\\/align\\]", "<div style=\"text-align: $1\">$2</div>"},
        {"\\[url=([^\\]]*)\\](.*?)\\[\\/url\\]", "<a href=\"$1\" target=\"_blank\">$2</a>"},
        {"\\[img\\]([^\\[]*?)\\[\\/img\\]", "<a href=\"$1\" target=\"_blank\"><img src=\"$1\" onload=\"cls.img.tResize(this, 600, 1800);\" /></a>"}
      };
      while (tState)
      {
        tState = false;
        for (int ti = 0; ti < tRegexAry.length; ti ++)
        {
          String tvalue1, tvalue2;
          Pattern tPattern = Pattern.compile(tRegexAry[ti][0]);
          Matcher tMatcher = tPattern.matcher(tString);
          while (tMatcher.find())
          {
            tState = true;
            tvalue1 = tMatcher.group();
            tvalue2 = tRegexAry[ti][1];
            for (int tk = 1; tk < (tMatcher.groupCount() + 1); tk ++) tvalue2 = tvalue2.replace("$" + tk, tMatcher.group(tk));
            tString = tString.replace(tvalue1, tvalue2);
          }
        }
      }
    }
    return tString;
  }
}